package com.makelove.bar.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.makelove.bar.entity.User;
import com.makelove.bar.mapper.UserMapper;
import com.makelove.bar.service.UserService;
import com.makelove.bar.util.ViewObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @program: bar
 * @description:
 * @author: 索腾龙
 * @create: 2021-04-28 17:30
 **/
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ViewObject<User> viewObject;
    @Override
    public User Login(User user) {
        //调用查询的方法
        User user1 = userMapper.selectOne(new QueryWrapper<>(user));
        //判断是否为空  如果不为空的话
        if (user1!=null){
            return user1;
        }
        return null;
    }
    //分页查询
    @Override
    public ViewObject<User> getAllUser(Integer page, Integer limit) {
        //创建Ipage对象   用有参构造 把当前页和每页显示的条数传进来
        IPage<User> iPage = new Page<User>(page,limit);
        //使用分页查询sql的方法
        IPage<User> result = userMapper.selectPage(iPage,null);
        //使用json的形式传到页面
        viewObject.setCode(0);
        viewObject.setMag("");
        viewObject.setCount(result.getTotal());
        viewObject.setData(result.getRecords());
        return viewObject;
    }
    //添加
    @Transactional
    @Override
    public Integer addUser(User user) {
        int insert = userMapper.insert(user);
        return insert;
    }

    @Transactional
    @Override
    public Integer updUser(User user) {
        return userMapper.updateById(user);
    }
    //删除
    @Transactional
    @Override
    public Integer del(Integer id) {
        return userMapper.deleteById(id);
    }


}